﻿using HtmlAgilityPack;
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            RequestOptions ro = new RequestOptions();
            var list = this.textBox1.Text;
            var allrul = list.Split("\r\n");

            var startid = 95;
            var title = "";
            var content = "";
            var allsql = "";

            foreach (var url in allrul)
            {
                ro.Uri = new Uri(url);
                ro.Method = "Get";
                string rs = RequestClass.RequestAction(ro);

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(rs);
                //取得标题

                title = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='word']").SelectSingleNode("p[2]").InnerText.Trim();
                title = title.Substring(1, title.Length - 2);

                //取得内容
                HtmlNodeCollection liNodes = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='word']").SelectNodes("p");
                if (liNodes != null && liNodes.Count > 0)
                {
                    for (int i = 0; i < liNodes.Count; i++)
                    {
                        if (liNodes[i].InnerText.Trim() == "延伸阅读") break;
                        content += liNodes[i].InnerText.Trim();
                        content += "\r\n\r\n";

                    }
                }


                var sql = $"insert into [main].[ay_content] values({startid}, 'cn', '7', '', '{title}', '#333333', '', '', '超级管理员', '本站', '', '2021-05-14 22:51:55', '', '', '{content}', '', '', '', '', 255, '1', '0', '0', '0', 2, 0, 0, 'admin', 'admin', '2021-05-14 22:52:42', '2021-05-14 22:52:42', '4', '0', '');\r\n";
                allsql += sql;
                startid++;
                content = "";
            }
            this.textBox2.Text = allsql;
        }
   
        private void button2_Click(object sender, EventArgs e)
        {
            (new Form2()).Show();
        }
    }
}
